package jia.core.calculations;

import jia.game.GenericFlags;
import org.apache.log4j.Logger;

import java.util.Random;

/**
 * This class handles all "Dice rolls" within the game.  Actions such as saving throws, attacks, spell
 * failure, etc are all determined by dice roll vs a difficulty rating.  This class performs the "roll"
 * which the calling method then handles the evaluation of the returned result.
 */
public class Dice{
    private static final Logger log = Logger.getLogger(Dice.class);
    private static final Random rnd = new Random(System.currentTimeMillis());
    
    /**
     * Static method that simulates an odd sided dice roll. The count attribute is
     * to specify the number of dice to roll
     *
     * @param count - the number of dice to be rolled.
     *
     * @return int value of the returned "roll", from 1 - the number specified
     */
    public static int roll(int sides, int count){
        int retVal = 0;
        if(count <= 0) {
            count = 1;
        }
        try {
            for(int i = 0; i < count; i++) {
                int tempVal = (int) (rnd.nextDouble() * sides) + 1;
                if (GenericFlags.debugMessages) {
                    log.debug("Rolled " + tempVal + " on a " + sides + "-sided die");
                }
                retVal = retVal + tempVal;
            }
        } catch(Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        return retVal;
    }
}
